﻿/**  版本信息模板在安装目录下，可自行修改。
* user.cs
*
* 功 能： N/A
* 类 名： user
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2016/9/29 星期四 17:14:24   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Utilities;//Please add references
using Beauty.Model;

namespace Beauty.DAL
{
    /// <summary>
    /// 数据访问类:user
    /// </summary>
    public partial class user
    {
        public user()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("Uid", "user");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Uid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from user");
            strSql.Append(" where Uid=@Uid");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@Uid", MySqlDbType.Int32)
            };
            parameters[0].Value = Uid;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Beauty.Model.user model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into user(");
            strSql.Append("UName,UPass,UMobile,UEmail,NickName,RegTime,IconUrl,State,qqOpenId,WxOpenId)");
            strSql.Append(" values (");
            strSql.Append("@UName,@UPass,@UMobile,@UEmail,@NickName,@RegTime,@IconUrl,@State,@qqOpenId,@WxOpenId)");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@UName", MySqlDbType.VarChar,255),
                    new MySqlParameter("@UPass", MySqlDbType.VarChar,255),
                    new MySqlParameter("@UMobile", MySqlDbType.VarChar,255),
                    new MySqlParameter("@UEmail", MySqlDbType.VarChar,255),
                    new MySqlParameter("@NickName", MySqlDbType.VarChar,255),
                    new MySqlParameter("@RegTime", MySqlDbType.DateTime),
                    new MySqlParameter("@IconUrl", MySqlDbType.VarChar,255),
                    new MySqlParameter("@State", MySqlDbType.VarChar,255),
                    new MySqlParameter("@qqOpenId", MySqlDbType.VarChar,255),
                    new MySqlParameter("@WxOpenId", MySqlDbType.VarChar,255)};
            parameters[0].Value = model.UName;
            parameters[1].Value = model.UPass;
            parameters[2].Value = model.UMobile;
            parameters[3].Value = model.UEmail;
            parameters[4].Value = model.NickName;
            parameters[5].Value = model.RegTime;
            parameters[6].Value = model.IconUrl;
            parameters[7].Value = model.State;
            parameters[8].Value = model.qqOpenId;
            parameters[9].Value = model.WxOpenId;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Beauty.Model.user model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update user set ");
            strSql.Append("UName=@UName,");
            strSql.Append("UPass=@UPass,");
            strSql.Append("UMobile=@UMobile,");
            strSql.Append("UEmail=@UEmail,");
            strSql.Append("NickName=@NickName,");
            strSql.Append("RegTime=@RegTime,");
            strSql.Append("IconUrl=@IconUrl,");
            strSql.Append("State=@State,");
            strSql.Append("qqOpenId=@qqOpenId,");
            strSql.Append("WxOpenId=@WxOpenId");
            strSql.Append(" where Uid=@Uid");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@UName", MySqlDbType.VarChar,255),
                    new MySqlParameter("@UPass", MySqlDbType.VarChar,255),
                    new MySqlParameter("@UMobile", MySqlDbType.VarChar,255),
                    new MySqlParameter("@UEmail", MySqlDbType.VarChar,255),
                    new MySqlParameter("@NickName", MySqlDbType.VarChar,255),
                    new MySqlParameter("@RegTime", MySqlDbType.DateTime),
                    new MySqlParameter("@IconUrl", MySqlDbType.VarChar,255),
                    new MySqlParameter("@State", MySqlDbType.VarChar,255),
                    new MySqlParameter("@qqOpenId", MySqlDbType.VarChar,255),
                    new MySqlParameter("@WxOpenId", MySqlDbType.VarChar,255),
                    new MySqlParameter("@Uid", MySqlDbType.Int32,11)};
            parameters[0].Value = model.UName;
            parameters[1].Value = model.UPass;
            parameters[2].Value = model.UMobile;
            parameters[3].Value = model.UEmail;
            parameters[4].Value = model.NickName;
            parameters[5].Value = model.RegTime;
            parameters[6].Value = model.IconUrl;
            parameters[7].Value = model.State;
            parameters[8].Value = model.qqOpenId;
            parameters[9].Value = model.WxOpenId;
            parameters[10].Value = model.Uid;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Uid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from user ");
            strSql.Append(" where Uid=@Uid");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@Uid", MySqlDbType.Int32)
            };
            parameters[0].Value = Uid;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Uidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from user ");
            strSql.Append(" where Uid in (" + Uidlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Beauty.Model.user GetModel(int Uid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Uid,UName,UPass,UMobile,UEmail,NickName,RegTime,IconUrl,State,qqOpenId,WxOpenId from user ");
            strSql.Append(" where Uid=@Uid");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@Uid", MySqlDbType.Int32)
            };
            parameters[0].Value = Uid;

            Beauty.Model.user model = new Beauty.Model.user();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Beauty.Model.user DataRowToModel(DataRow row)
        {
            Beauty.Model.user model = new Beauty.Model.user();
            if (row != null)
            {
                if (row["Uid"] != null && row["Uid"].ToString() != "")
                {
                    model.Uid = int.Parse(row["Uid"].ToString());
                }
                if (row["UName"] != null)
                {
                    model.UName = row["UName"].ToString();
                }
                if (row["UPass"] != null)
                {
                    model.UPass = row["UPass"].ToString();
                }
                if (row["UMobile"] != null)
                {
                    model.UMobile = row["UMobile"].ToString();
                }
                if (row["UEmail"] != null)
                {
                    model.UEmail = row["UEmail"].ToString();
                }
                if (row["NickName"] != null)
                {
                    model.NickName = row["NickName"].ToString();
                }
                if (row["RegTime"] != null && row["RegTime"].ToString() != "")
                {
                    model.RegTime = DateTime.Parse(row["RegTime"].ToString());
                }
                if (row["IconUrl"] != null)
                {
                    if (row["IconUrl"].ToString().IndexOf("http://") == -1)
                    {
                        model.IconUrl = string.Format("{1}{0}", row["IconUrl"].ToString(), DataConfig.Domain);
                    }
                    else
                    {
                        model.IconUrl = string.Format("{0}", row["IconUrl"].ToString());
                    }
                }
                if (row["State"] != null)
                {
                    model.State = row["State"].ToString();
                }
                if (row["qqOpenId"] != null && row["qqOpenId"].ToString() != "")
                {
                    model.qqOpenId = row["qqOpenId"].ToString();
                }
                if (row["WxOpenId"] != null && row["WxOpenId"].ToString() != "")
                {
                    model.WxOpenId = row["WxOpenId"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Uid,UName,UPass,UMobile,UEmail,NickName,RegTime,IconUrl,State,qqOpenId,WxOpenId ");
            strSql.Append(" FROM user ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM user ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperMySQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Uid desc");
            }
            strSql.Append(")AS Row, T.*  from user T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "user";
			parameters[1].Value = "Uid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

